package com.aij.starter.jna;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;

public class Hello {

    static String url = "";
    static HelloLibrary HELLO;
    static {
		try {
			System.setProperty("jna.encoding", "UTF-8");
			String os = System.getProperty("os.name").toLowerCase();
			if (os.startsWith("windows"))
			{
				url = ClassLoader.getSystemResource("libs/libhello.dll").getPath();
			} else if (os.startsWith("mac")) {
				url = ClassLoader.getSystemResource("libs/libhello.dylib").getPath();
			} else if (os.startsWith("linux")) {
				url = ClassLoader.getSystemResource("libs/libhello.so").getPath();
			} else {
				throw new Exception("Not support system: " + os);
			}
			HELLO = (HelloLibrary)Native.loadLibrary(url, HelloLibrary.class);
		} catch (Exception e) {
			System.out.println(e.getMessage() + "\n" + e.getStackTrace());
		}
    }
	
	public interface HelloLibrary extends Library {
		
		void sayHello();
	    
	    class Point extends Structure {
	        public static class ByValue extends Point implements Structure.ByValue { }
	        public int x, y;
	    }
	    Point.ByValue translate(Point.ByValue pt, int x, int y);
	}
 
    // Test Driver
    public static void main(String[] args) {
    	HELLO.sayHello();  // Create an instance and invoke the native method
    	
    	HelloLibrary.Point.ByValue pt = new HelloLibrary.Point.ByValue();
    	pt.x = 1;
    	pt.y = 1;
    	HelloLibrary.Point.ByValue result = HELLO.translate(pt, 100, 100);
    	System.out.println(result.x + "");
    }
}
